package yf.mall.controller.api.app;


import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.entity.AppFavoriteEntity;
import yf.mall.enums.app.AppCollectTypeEnum;
import yf.mall.service.app.AppFavoriteService;
import yf.mall.vo.app.request.CollectRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/app/mini/collect")
public class ApiAppCollectionController {

    @Resource
    private AppFavoriteService appFavoriteService;

    @RequestMapping(value = "/add",
            method = RequestMethod.POST)
    public R addCollect(@LoginUserId Long userId, @RequestBody CollectRequest collectRequest) {
        if (collectRequest == null || collectRequest.getTargetId() == null) {
            return R.error("收藏主体信息为空");
        }
        AppCollectTypeEnum collectTypeEnum = AppCollectTypeEnum.getByKey(collectRequest.getTargetType());
        if (collectTypeEnum == null) {
            return R.error("不支持的收藏类型");
        }
        AppFavoriteEntity query = new AppFavoriteEntity();
        query.setCollectorId(userId);
        query.setRefId(collectRequest.getTargetId());
        query.setType(collectTypeEnum.getKey());
        query.setStatus(0);
        List<AppFavoriteEntity> favoriteEntities = appFavoriteService.find(query);
        if (favoriteEntities.isEmpty()) {
            AppFavoriteEntity entity = new AppFavoriteEntity();
            entity.setCollectorId(userId);
            entity.setCollectorName("登录用户：" + userId);
            entity.setType(collectTypeEnum.getKey());
            entity.setRefId(collectRequest.getTargetId());
            entity.setStatus(1);
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            appFavoriteService.save(entity);
            return R.ok();
        }
        AppFavoriteEntity entity = new AppFavoriteEntity();
        entity.setId(favoriteEntities.get(0).getId());
        entity.setStatus(1);
        int result = appFavoriteService.update(entity);
        return R.ok(result > 0);
    }

    @RequestMapping(value = "/cancel",
            method = RequestMethod.POST)
    public R cancelCollect(@LoginUserId Long userId, @RequestBody CollectRequest collectRequest) {
        if (collectRequest == null || collectRequest.getTargetId() == null) {
            return R.error("收藏主体信息为空");
        }
        log.info("用户{}操作取消收藏，收藏对象{}，收藏类型{}", userId, collectRequest.getTargetId(), collectRequest.getTargetType());
        AppCollectTypeEnum collectTypeEnum = AppCollectTypeEnum.getByKey(collectRequest.getTargetType());
        if (collectTypeEnum == null) {
            return R.error("不支持的收藏类型");
        }
        AppFavoriteEntity query = new AppFavoriteEntity();
        query.setCollectorId(userId);
        query.setRefId(collectRequest.getTargetId());
        query.setType(collectTypeEnum.getKey());
        query.setStatus(1);
        List<AppFavoriteEntity> favoriteEntities = appFavoriteService.find(query);
        if (favoriteEntities.isEmpty()) {
            log.info("用户{}数据库中取消收藏对象不存在，暂时不处理，收藏对象{}，收藏类型{}", userId, collectRequest.getTargetId(), collectRequest.getTargetType());
            return R.ok();
        }
        AppFavoriteEntity entity = new AppFavoriteEntity();
        entity.setId(favoriteEntities.get(0).getId());
        entity.setStatus(0);
        int result = appFavoriteService.update(entity);
        return R.ok(result > 0);
    }
}
